/**
 * <p>Title: MainMenu.java</p>
 * <p>Description: CS 343 Final Project. This class shows a main menu when user starts the game</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package GameState;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import Components.MoveableBackground;
import Components.Music;
import Components.SoundEffects;
import Main.Begin;
import Settings.Config;

public class MainMenu extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1010606246866878102L;
	private JButton StartGame;
	private JButton LeaderBoard;
	private JButton Credits;
	private JButton Help;
	private JButton AudioButton;
	private SoundEffects soundFX;
	private boolean muted = false;
	private MoveableBackground background;
	private Image filterImage = new ImageIcon("src/images/filter.png")
			.getImage();
	private Image logo = new ImageIcon("src/images/supersegway3000.png")
			.getImage();

	/**
	 * @param none
	 * @description sets the main menu when game is launched
	 */
	private static Music backgroundMusic = new Music();

	@SuppressWarnings("static-access")
	public MainMenu() {
		background = new MoveableBackground();
		// Starts the background music to run as a separate thread

		if (!backgroundMusic.isMuted()) {
			try {
				if (!backgroundMusic.isPlaying())
					backgroundMusic.gameMainMenuMusic();
			} catch (IOException e1) {
				System.err.println("Could not play music - IOException");
			} catch (Exception e1) {
				System.err.println("Could not play music - Exception");
			}
		}
		soundFX = new SoundEffects();

		setLayout(null);

		if (!backgroundMusic.isMuted()) {
			AudioButton = new JButton(new ImageIcon("src/images/Sound.png"));
			AudioButton.setRolloverIcon(new ImageIcon(
					"src/images/soundHover.png"));
		} else {
			AudioButton = new JButton(new ImageIcon("src/images/mute.png"));
			AudioButton.setRolloverIcon(new ImageIcon(
					"src/images/muteHover.png"));
		}
		AudioButton.setBounds(Config.FRAME_WIDTH - 60, 10, 50, 50);
		// removes background of button
		AudioButton.setContentAreaFilled(false);
		// removes button border
		AudioButton.setBorder(BorderFactory.createEmptyBorder());
		add(AudioButton);
		AudioButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (muted) {
					AudioButton.setIcon(new ImageIcon("src/images/sound.png"));
					AudioButton.setRolloverIcon(new ImageIcon(
							"src/images/soundHover.png"));
					try {
						backgroundMusic.gameMainMenuMusic();
					} catch (IOException e1) {
						e1.printStackTrace();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				} else {
					AudioButton.setIcon(new ImageIcon("src/images/mute.png"));
					AudioButton.setRolloverIcon(new ImageIcon(
							"src/images/muteHover.png"));
					backgroundMusic.stopDaBeats();
				}
				muted = !muted;
				backgroundMusic.switchMuted();
				
				if (muted) {
					soundFX.mute();
				} else {
					soundFX.unmute();
				}
			}
		});

		// Start Game button Set up
		StartGame = new JButton(new ImageIcon("src/images/play.png"));
		StartGame.setRolloverIcon(new ImageIcon("src/images/PlayHover.png"));
		StartGame.setBounds(25, 335, 184, 200);

		// removes background of button
		StartGame.setContentAreaFilled(false);
		// removes button border
		StartGame.setBorder(BorderFactory.createEmptyBorder());
		StartGame.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
				soundFX.playTickSound();
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		});
		StartGame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (!backgroundMusic.isMuted())
					backgroundMusic.stopDaBeats();
				Begin.app.setGameState(1);
			}

		});
		add(StartGame);

		// Leader Board button set up
		LeaderBoard = new JButton(new ImageIcon("src/images/leaders.png"));
		LeaderBoard
				.setRolloverIcon(new ImageIcon("src/images/LeadersHover.png"));
		LeaderBoard.setBounds(250, 335, 184, 200);

		// removes background of button
		LeaderBoard.setContentAreaFilled(false);
		// removes button border
		LeaderBoard.setBorder(BorderFactory.createEmptyBorder());
		LeaderBoard.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
				soundFX.playTickSound();
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		});
		LeaderBoard.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Begin.app.setGameState(2);
			}

		});
		add(LeaderBoard);

		// Credits button set up
		Credits = new JButton(new ImageIcon("src/images/Credits.png"));
		Credits.setRolloverIcon(new ImageIcon("src/images/CreditsHover.png"));
		Credits.setBounds(475, 335, 184, 200);

		// removes background of button
		Credits.setContentAreaFilled(false);
		// removes button border
		Credits.setBorder(BorderFactory.createEmptyBorder());
		Credits.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
				soundFX.playTickSound();
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		});
		Credits.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Begin.app.setGameState(4);
			}

		});
		add(Credits);

		// Options Button set up
		Help = new JButton(new ImageIcon("src/images/help.png"));
		Help.setRolloverIcon(new ImageIcon("src/images/helpHover.png"));
		Help.setBounds(700, 335, 184, 200);

		// removes background of button
		Help.setContentAreaFilled(false);
		// removes button border
		Help.setBorder(BorderFactory.createEmptyBorder());
		Help.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
				soundFX.playTickSound();
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		});
		Help.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Begin.app.setGameState(3);
			}

		});
		add(Help);

	}

	/**
	 * @param g
	 * @description paints the graphics of the main menu items and images
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(background.getBlue(), 0, 0, null);

		g2d.drawImage(background.getBackClouds1(),
				background.getX1backClouds(), background.getY1backClouds(),
				null);
		g2d.drawImage(background.getBackClouds2(),
				background.getX2backClouds(), background.getY2backClouds(),
				null);

		g2d.drawImage(background.getForeClouds1(),
				background.getX1foreClouds(), background.getY1foreClouds(),
				null);
		g2d.drawImage(background.getForeClouds2(),
				background.getX2foreClouds(), background.getY2backClouds(),
				null);

		// background
		if (background.getX1() < Config.FRAME_WIDTH)
			g2d.drawImage(background.getBackgroundImage1(), background.getX1(),
					background.getY1(), null);
		if (background.getX2() < Config.FRAME_WIDTH)
			g2d.drawImage(background.getBackgroundImage2(), background.getX2(),
					background.getY2(), null);

		g2d.drawImage(filterImage, 0, 0, null);
		g2d.drawImage(logo, (Config.FRAME_WIDTH - logo.getWidth(null)) / 2, 40,
				null);
	}

	@Override
	/**
	 * @param arg0
	 * @description repaints for the main menu
	 */
	public void actionPerformed(ActionEvent arg0) {
		repaint();
	}

}
